<?php

/**
 * Created by PhpStorm.
 * User: 王军
 * Date: 2017/3/22 0022
 * Time: 上午 11:42
 */
namespace Admin\Model;
use Think\Model;
class XlExpendModel extends Model{
    protected $cats;
    public function __construct(){
        parent::__construct();
//        $redis = linkredis();
//        if($catinfo = $redis->get('catinfo')){
//            $this->cats = unserialize($catinfo);
//        }else {
            $this->cats = unserialize(file_get_contents('./Public/catinfo.txt'));
        //}
    }
    //定义表单能接收哪些字段
    protected $insertFields = 'cpxlno,xlmc,intro,skill,sm,jghsm,ct_intro,qt_intro';
    protected $updateFields = 'id,cpxlno,xlmc,intro,skill,sm,jghsm,ct_intro,qt_intro';

    //自动验证
    protected $_validate = array(
        array('cpxlno','require','必须选择一个系列',1),
    );

    //表单添加之前的钩子函数
    protected function _before_insert(&$data,$option){
        /******************添加降功耗图片和订货示例图*********************/
        if($_FILES['jgh_img']['error'] == 0){
            $info = uploadfile('jgh_img','jgh');
            if($info['type'] === 0){
                $this->error = $info['error'];
                return false;
            }else{
                $data['jgh_img'] = $info;
            }
        }
        if($_FILES['dhsl_img']['error'] == 0){
            $info = uploadfile('dhsl_img','dhsl');
            if($info['type'] === 0){
                $this->error = $info['error'];
                return false;
            }else{
                $data['dhsl_img'] = $info;
            }
        }
        /*********************转换技术标准和说明数据***************************/
        $skill = I('post.skill');
        $sm = I('post.sm');
        $data['skill'] = implode('@@',$skill);
        $data['sm'] = implode('@@',$sm);
        $data['addtime'] = time();
        //dump($data);exit;
    }

    //表单添加之后的钩子函数
    protected function _after_insert($data,$option){
        $file = $_FILES;  //把上传数据先赋给一个变量
        $cpxlno = I('post.cpxlno');
        /******************添加产品相册表的数据*********************/
        if($_FILES['xl_img']['error'][0] === 0){
            $xl_img = $this->files('xl_img');
            //dump($_FILES);exit;
            foreach($xl_img as $k=>$v){
                if($v['error'] == 0){
                    $rst = uploadfile($k,'xlimg');
                    //dump($rst);exit;
                    if($rst['type'] === 0){
                        $this->error = $rst['error'];
                        return false;
                    }else{
                        $xl_img = D('xl_img');
                        $xl_img->add(array(
                            'cpxlno' => $cpxlno,
                            'img' => $rst
                        ));
                    }
                }
            }
        }
        $_FILES = $file; //当一个表的上传文件上传完成之后,在把保存上传文件的变量赋回给$_FILES,因为下一个上传还需要用到$_FILES
        //dump($_FILES);exit;
        /******************添加外形尺寸和特性表的数据*********************/
        if($_FILES['ct_img']['error'][0] === 0){
            $ct_img = $this->files('ct_img');
            //dump($_FILES);exit;
            $ct_intro = I('post.ct_intro');
            foreach($ct_img as $k=>$v){
                if($v['error'] == 0){
                    $rst = uploadfile($k,'ctimg');
                    //dump($rst);exit;
                    if($rst['type'] === 0){
                        $this->error = $rst['error'];
                        return false;
                    }else{
                        $size_img = D('size_img');
                        $size_img->add(array(
                            'cpxlno' => $cpxlno,
                            'ct_img' => $rst,
                            'ct_intro' => $ct_intro[$k]
                        ));
                    }
                }
            }
        }
        $_FILES = $file; //当一个表的上传文件上传完成之后,在把保存上传文件的变量赋回给$_FILES,因为下一个上传还需要$_FILES里获取数据
        //dump($_FILES);exit;
        /******************添加其他图谱表的数据*********************/
        $qt_name = I('post.qt_name');
        if($_FILES['qt_img']['error'][0] === 0 && $qt_name[0] != '') {
            $qt_img = $this->files('qt_img');
            //dump($_FILES);exit;
            $qt_intro = I('post.qt_intro');
            foreach($qt_img as $k=>$v){
                if($v['error'] == 0){
                    $rst = uploadfile($k,'qtimg');
                    //dump($rst);exit;
                    if($rst['type'] === 0){
                        $this->error = $rst['error'];
                        return false;
                    }else{
                        $xl_other_img = D('xl_other_img');
                        $xl_other_img->add(array(
                            'cpxlno' => $cpxlno,
                            'qt_name' => $qt_name[$k],
                            'qt_img' => $rst,
                            'qt_intro' => $qt_intro[$k]
                        ));
                    }
                }
            }
        }
        /******************添加电参数表的数据*********************/
        $diancan = I('post.diancan');
        $sign = I('post.sign');
        $zdy_dcs = I('post.zdy_dcs');
        $zdy_sign = I('post.zdy_sign');
        $zdy_dcsval = I('post.zdy_dcsval');
        $xl_diancs = D('xl_diancs');
        foreach($diancan as $k=>$v){
            $_k = explode('---',$k);
            $xl_diancs->add(array(
                'cpxlno' => $cpxlno,
                'electriparam_id' => $_k[0],
                'electriparam_name' => $_k[1],
                'electriparam_sign' => $sign[$k],
                'electriparam_val' => $v
            ));
        }
        if($zdy_dcs[0] != ''){
            foreach($zdy_dcs as $k=>$v){
                $_v = explode('---',$v);
                $xl_diancs->add(array(
                    'cpxlno' => $cpxlno,
                    'electriparam_id' => $_v[0],
                    'electriparam_name' => $_v[1],
                    'electriparam_sign' => $zdy_sign[$k],
                    'electriparam_val' => $zdy_dcsval[$k],
                ));
            }
        }

    }

    //表单修改之前的钩子函数
    protected function _before_update(&$data,$option){
        //dump($data);exit;
        $id = $option['where']['id'];
        /******************修改降功耗图片和订货示例图*********************/
        //降功耗图片
        if($_FILES['jgh_img']['error'] === 0){
            $info = uploadfile('jgh_img','jgh');
            if($info['type'] === 0){
                $this->error = $info['error'];
                return false;
            }else{
                $data['jgh_img'] = $info;
                delOneimg($id,'xl_expend','jgh_img');
            }
        }
        //订货示例图
        if($_FILES['dhsl_img']['error'] === 0){
            $info = uploadfile('dhsl_img','dhsl');
            if($info['type'] === 0){
                $this->error = $info['error'];
                return false;
            }else{
                $data['dhsl_img'] = $info;
                delOneimg($id,'xl_expend','dhsl_img');
            }
        }
        /*********************修改技术标准和说明数据***************************/
        $skill = I('post.skill');
        $sm = I('post.sm');
        $data['skill'] = implode('@@',$skill);
        $data['sm'] = implode('@@',$sm);
        //dump($data);exit;
    }

    //表单修改之后的钩子函数
    public function _after_update($data,$option){
//        dump(I('post.'));
//        dump($_FILES);exit;
        $file = $_FILES;  //把上传数据先赋给一个变量
        $cpxlno = I('post.cpxlno');
        if($_FILES['xl_img']['error'][0] === 0){
            $xl_img = $this->files('xl_img');
            //dump($_FILES);exit;
            foreach($xl_img as $k=>$v){
                if($v['error'] == 0){
                    $rst = uploadfile($k,'xlimg');
                    //dump($rst);exit;
                    if($rst['type'] === 0){
                        $this->error = $rst['error'];
                        return false;
                    }else{
                        $xl_img = D('xl_img');
                        $xl_img->add(array(
                            'cpxlno' => $cpxlno,
                            'img' => $rst
                        ));
                    }
                }
            }
        }
        $_FILES = $file; //当一个表的上传文件上传完成之后,在把保存上传文件的变量赋回给$_FILES,因为下一个上传还需要用到$_FILES
        //dump($_FILES);exit;
        /******************添加外形尺寸和特性表的数据*********************/
        if($_FILES['ct_img']['error'][0] === 0){
            $ct_img = $this->files('ct_img');
            //dump($_FILES);exit;
            $ct_intro = I('post.ct_intro');
            foreach($ct_img as $k=>$v){
                if($v['error'] == 0){
                    $rst = uploadfile($k,'ctimg');
                    //dump($rst);exit;
                    if($rst['type'] === 0){
                        $this->error = $rst['error'];
                        return false;
                    }else{
                        $size_img = D('size_img');
                        $size_img->add(array(
                            'cpxlno' => $cpxlno,
                            'ct_img' => $rst,
                            'ct_intro' => $ct_intro[$k]
                        ));
                    }
                }
            }
        }
        $_FILES = $file; //当一个表的上传文件上传完成之后,在把保存上传文件的变量赋回给$_FILES,因为下一个上传还需要$_FILES里获取数据
        //dump($_FILES);exit;
        /******************添加其他图谱表的数据*********************/
        $qt_name = I('post.qt_name');
        if($_FILES['qt_img']['error'][0] === 0 && $qt_name[0] != '') {
            $qt_img = $this->files('qt_img');
            //dump($_FILES);exit;
            $qt_intro = I('post.qt_intro');
            foreach($qt_img as $k=>$v){
                if($v['error'] == 0){
                    $rst = uploadfile($k,'qtimg');
                    //dump($rst);exit;
                    if($rst['type'] === 0){
                        $this->error = $rst['error'];
                        return false;
                    }else{
                        $xl_other_img = D('xl_other_img');
                        $xl_other_img->add(array(
                            'cpxlno' => $cpxlno,
                            'qt_name' => $qt_name[$k],
                            'qt_img' => $rst,
                            'qt_intro' => $qt_intro[$k]
                        ));
                    }
                }
            }
        }
        /******************修改电参数表的数据*********************/
        $xl_diancs = D('xl_diancs');
        $dianc_name = I('post.dcname');
        $sign = I('post.sign');
        $dcsval = I('post.dcsval');
        $zdy_dcs = I('post.zdy_dcs');
        $zdy_sign = I('post.zdy_sign');
        $zdy_dcsval = I('post.zdy_dcsval');
        $xl_diancs->where("cpxlno={$cpxlno}")->delete();
        foreach($dianc_name as $k=>$v){
            $xl_diancs->add(array(
                'cpxlno' => $cpxlno,
                'electriparam_id' => $k,
                'electriparam_name' => $v,
                'electriparam_sign' => $sign[$k],
                'electriparam_val' => $dcsval[$k],
            ));
        }
        if($zdy_dcs[0] != ''){
            foreach($zdy_dcs as $k=>$v){
                $_v = explode('---',$v);
                $xl_diancs->add(array(
                    'cpxlno' => $cpxlno,
                    'electriparam_id' => $_v[0],
                    'electriparam_name' => $_v[1],
                    'electriparam_sign' => $zdy_sign[$k],
                    'electriparam_val' => $zdy_dcsval[$k],
                ));
            }
        }
    }

    protected function _before_delete($option){
        $id = $option['where']['id'];
        /****************删除降功耗图片和订货示例图*******************/
        delOneimg($id,'xl_expend','jgh_img');
        delOneimg($id,'xl_expend','dhsl_img');

        $info = $this->field('cpxlno')->find($id);
        $cpxlno = $info['cpxlno'];
        $where['cpxlno'] = $cpxlno;
        $xl_img = D('xl_img');
        $size_img = D('size_img');
        $qt_other_img = D('xl_other_img');

        /******************删除系列相册,尺寸和特性,其他图谱的图片***********************/
        delAllimg($cpxlno,$xl_img,'img');
        delAllimg($cpxlno,$size_img,'ct_img');
        delAllimg($cpxlno,$qt_other_img,'qt_img');

        /******************删除系列相册,尺寸和特性,其他图谱表的数据***********************/
        $xl_img->where($where)->delete();
        $size_img->where($where)->delete();
        $qt_other_img->where($where)->delete();



    }


    /**
     * 把一个类型上传文件转换成$_FILES类型的文件
     * @param $img_type  上传的文件名称
     * @return array
     */
    private function files($img_type){
        $arr = [];
        foreach ($_FILES[$img_type]['name'] as $k => $v) {
            $arr[] = array(
                'name' => $v,
                'type' => $_FILES[$img_type]['type'][$k],
                'tmp_name' => $_FILES[$img_type]['tmp_name'][$k],
                'error' => $_FILES[$img_type]['error'][$k],
                'size' => $_FILES[$img_type]['size'][$k],
            );
        }
        $_FILES = $arr; //把循环后的上传数据赋给$_FILES,因为上传类要在$_FILES里获取数据
        return $arr;
    }
    //获取第二级分类
    public function getcatinfo(){
        $arr = [];
        foreach($this->cats as $k=>$v){
            if($v['lastcat'] != false){
                $arr[$v['flno']] = $v['flmc'];
            }
        }
        return $arr;
    }
    //获取最后一级分类
    public function getcatlast($flno){
        $arr = [];
        //dump($this->cats);exit;
        foreach($this->cats as $k=>$v){
            if($v['flno'] == $flno){
                $arr = $v['lastcat'];
            }
        }
        return $arr;
    }
    //获取系列名称
    public function getcatxl($flno){
        /*****************************取出系列*****************************/
        $cpxl = D('Home/b_p_cpxl');
        $cpxlnos = $this->field('cpxlno')->select();
        $cpxlnos = implode(',',array_column($cpxlnos,'cpxlno'));
        if($cpxlnos){
            $where = "cpxlno not in ($cpxlnos)";
        }else{
            $where = 1;
        }
        $cpxlinfo = $cpxl->field('cpxlno,xpxlmc')->where("flno={$flno} and {$where}")->select();
        return array(
            'cpxlinfo' => $cpxlinfo,
        );
    }

    //获取所有的电参数
    public function getdiancanshuAll(){
        $d_cc_electriparam = D('Admin/d_cc_electriparam');
        $diancanshu_info = $d_cc_electriparam->field('id,name')->select();
        return $diancanshu_info;
    }

    //获取单个分类下的电参数
    public function getdiancanshu($flno){
        $sql = "select c.id,c.name from d_cc_fleptemplate a
                join d_cc_eptemplategx b on a.templateid=b.templateid
                join d_cc_electriparam c on b.epid=c.id
                where a.flno={$flno}";
        $diancanshu_info = D()->query($sql);
        //去掉name中的换行符
        foreach($diancanshu_info as $k=>&$v){
            $v['name'] =str_replace(array("\r\n", "\r", "\n"), "", $v['name']);

        }
        return $diancanshu_info;
    }

    public function search(){
        $xlmc = I('get.xlmc');
        $where = '';
        if($xlmc){
            $xlmc = strtoupper($xlmc);
            $where['xlmc'] = array('like',"%".$xlmc."%");
        }
        $count = $this->where($where)->count();
        $page = new \Think\Page($count,15);
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page_str = $page->show();
        $list = $this->field('id,cpxlno,xlmc,intro,skill,addtime')
            ->where($where)->limit($page->firstRow,$page->listRows)->order('addtime desc')->select();
        //dump($this->getLastSql());exit;
        foreach($list as &$v){
            $v['addtime'] = date('Y-m-d',$v['addtime']);
        }
        return array(
            'page_str' => $page_str,
            'list' => $list
        );
    }

    public function search_one($cpxlno){
        $list = $this->where("cpxlno={$cpxlno}")->find();
        $list['skill'] = explode('@@',$list['skill']);
        $list['sm'] = explode('@@',$list['sm']);
        $list['img'] = D()->query("select id,img from xl_img where cpxlno={$cpxlno}");
        $list['ct'] = D()->query("select id,ct_img,ct_intro from size_img where cpxlno={$cpxlno}");
        $list['qt'] = D()->query("select id,qt_name,qt_intro,qt_img from xl_other_img where cpxlno={$cpxlno}");
        $list['dianc'] = D()->query("select electriparam_id,electriparam_name,electriparam_sign,electriparam_val from xl_diancs where cpxlno={$cpxlno}");
        return $list;
    }


}